<script type="text/javascript">//<![CDATA[
function cb(v) {
 var s='<input type="checkbox" disabled="disabled" ';
 if (v==1)
   s=s+'checked="checked" ';
 return s+'/>';  
}
function fmtduration(duration) {
  seconds=parseInt(duration);
  if (seconds<0)
    return "";
  var hh = Math.floor(seconds / 3600),
      mm = Math.floor(seconds / 60) % 60,
      ss = Math.floor(seconds) % 60;
  return (hh ? (hh < 10 ? "0" : "") + hh + ":" : "") + ((mm < 10) && hh ? "0" : "") + mm + ":" + (ss < 10 ? "0" : "") + ss
}
function callname(incoming,outgoing) {
  if (parseInt(incoming)==1)
    return "<%:incoming%>";
  if (parseInt(outgoing)==1)
    return "<%:outgoing%>";
  return "";    
}
XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "svd", "status")%>', null,
        function(x, repl)
        {
                var tb = document.getElementById('svd_accounts_status_table');
                var st = repl.regs;
                var chans = st.chans;
                if (st && tb)
                {
                        var i;
                        for (i=tb.rows.length-1; i>0; i--) 
                          tb.deleteRow(i);
                        
                        for (i=0; i<st.length; i++) 
                        {  
                          var tr = tb.insertRow(-1);
                          tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);

                          tr.insertCell(-1).innerHTML = st[i].account;
                          tr.insertCell(-1).innerHTML = cb(st[i].enabled);
                          tr.insertCell(-1).innerHTML = st[i].uri;
                          tr.insertCell(-1).innerHTML = cb(st[i].registered);
                          tr.insertCell(-1).innerHTML = st[i].last_message;
                        }  
                }
                st = repl.chans;
                tb = document.getElementById('svd_channels_status_table');
                if (st && tb)
                {
                        var i;
                        for (i=tb.rows.length-1; i>0; i--) 
                          tb.deleteRow(i);
                        
                        for (i=0; i<st.length; i++) 
                        {  
                          var tr = tb.insertRow(-1);
                          tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);

                          tr.insertCell(-1).innerHTML = st[i].channel;
                          tr.insertCell(-1).innerHTML = cb(st[i].offhook);
                          tr.insertCell(-1).innerHTML = callname(st[i].incoming, st[i].outgoing);
                          tr.insertCell(-1).innerHTML = st[i].account;
                          tr.insertCell(-1).innerHTML = st[i].address;
                          tr.insertCell(-1).innerHTML = fmtduration(st[i].duration);
                        }  
                }
        }
);
//]]></script>
<fieldset class="cbi-section">
        <legend><%:Current Voip State%></legend>
        <h3><%:Accounts%></h3>
        <table class="cbi-section-table" id="svd_accounts_status_table">
                <tr class="cbi-section-table-titles">
                        <th class="cbi-section-table-cell"><%:Account%></th>
                        <th class="cbi-section-table-cell"><%:Enabled%></th>
                        <th class="cbi-section-table-cell"><%:Uri%></th>
                        <th class="cbi-section-table-cell"><%:Registered%></th>
                        <th class="cbi-section-table-cell"><%:Last message%></th>
                </tr>
                <tr class="cbi-section-table-row">
                        <td colspan="5"><em><br /><%:Collecting data...%></em></td>
                </tr>
        </table>
        <h3><%:Channels%></h3>
        <table class="cbi-section-table" id="svd_channels_status_table">
                <tr class="cbi-section-table-titles">
                        <th class="cbi-section-table-cell"><%:Channel%></th>
                        <th class="cbi-section-table-cell"><%:Off hook%></th>
                        <th class="cbi-section-table-cell"><%:Call%></th>
                        <th class="cbi-section-table-cell"><%:Account%></th>
                        <th class="cbi-section-table-cell"><%:To-From%></th>
                        <th class="cbi-section-table-cell"><%:Duration%></th>
                </tr>
                <tr class="cbi-section-table-row">
                        <td colspan="5"><em><br /><%:Collecting data...%></em></td>
                </tr>
        </table>
</fieldset>
